package com.hejjon.domain.pojo;

import com.alibaba.fastjson.JSON;
import com.hejjon.core.domain.entity.DBConnection;
import com.hejjon.core.utils.AESUtil;
import com.hejjon.enumer.DriverEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author: cs
 * @date: 2025-02-21 17:30:34
 * @since: 1.0
 */
@Data
public class ConnectionInfo implements Serializable {


    private String username;

    private String password;

    private String driver;

    private String url;

    public ConnectionInfo() {
    }

    public ConnectionInfo(DBConnection conn) {
        this.username = conn.getUsername();
        this.password = conn.getPassword();
        this.url = conn.getUrl();
        this.setDriver(DriverEnum.getDriverByType(conn.getDriveType()));
    }




    public static ConnectionInfo of(DBConnection dbConn, boolean decrypt) {
        ConnectionInfo info = new ConnectionInfo();
        info.setUsername(dbConn.getUsername());
        info.setDriver(DriverEnum.getDriverByType(dbConn.getDriveType()));
        info.setUrl(dbConn.getUrl());
        // 是否需要解密密码, 持久化对象DBConnection 的密码都是密文
        if (decrypt) {
            String decryptPwd = AESUtil.decryptByAES(dbConn.getPassword());
            info.setPassword(decryptPwd);
        } else {
            info.setPassword(dbConn.getPassword());
        }
        return info;
    }


    public String toJson() {
        return JSON.toJSONString(this);
    }




}
